package com.huanlis.cloud.utils;

import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 通用返回结果，支持链式调用
 *
 * @author lihuan
 * @since 2023/10/18 14:49:19
 */
@Schema(description = "数据返回对象")
public class R {

    /**
     * 业务响应状态
     */
    @Schema(description = "业务响应状态")
    private Boolean success;

    /**
     * 业务响应状态码
     */
    @Schema(description = "业务响应状态码")
    private Integer code;

    /**
     * 业务响应信息
     */
    @Schema(description = "业务响应信息")
    private String message;

    /**
     * 业务返回结果
     */
    @Schema(description = "业务返回结果")
    private Object data;


    /**
     * 私有化构造器
     *
     * @author lihuan
     * @since 2023/10/18 15:25
     */
    private R() {

    }

    /**
     * 构造成功返回结果
     *
     * @author lihuan
     * @since 2023/10/18 15:45
     */
    public static R success() {
        R r = new R();
        r.setSuccess(RCodeEnum.SUCCESS.getSuccess());
        r.setCode(RCodeEnum.SUCCESS.getCode());
        r.setMessage(RCodeEnum.SUCCESS.getMessage());
        return r;
    }

    /**
     * 构造成功返回结果
     *
     * @author lihuan
     * @since 2023/10/18 15:45
     */
    public static R success(Object data) {
        R r = new R();
        r.setSuccess(RCodeEnum.SUCCESS.getSuccess());
        r.setCode(RCodeEnum.SUCCESS.getCode());
        r.setMessage(RCodeEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }


    /**
     * 构建失败返回结果
     *
     * @author lihuan
     * @since 2023/10/18 15:45
     */
    public static R fail() {
        R r = new R();
        r.setSuccess(RCodeEnum.HTTP_CLIENT_ERROR.getSuccess());
        r.setCode(RCodeEnum.HTTP_CLIENT_ERROR.getCode());
        r.setMessage(RCodeEnum.HTTP_CLIENT_ERROR.getMessage());
        return r;
    }

    /**
     * 链式设置响应数据
     *
     * @param data 响应数据
     * @return com.huanlis.cloud.utils.R
     * @author lihuan
     * @since 2023/10/18 15:46
     */
    public R data(Object data) {
        this.setData(data);
        return this;
    }

    /**
     * 链式设置业务响应码
     *
     * @param code 业务响应码
     * @return com.huanlis.cloud.utils.R
     * @author lihuan
     * @since 2023/10/18 15:47
     */
    public R code(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * 链式设置业务响应信息
     *
     * @param message 业务响应信息
     * @return com.huanlis.cloud.utils.R
     * @author lihuan
     * @since 2023/10/18 15:47
     */
    public R message(String message) {
        this.message = message;
        return this;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }
}
